﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;


namespace Extractor.LibCode
{
    class cTeam : cDBService
    {
        public string TEAM_HEADER_NAME = "TBTEAM";
        private cMapping oMapping = new cMapping();

        private string nickname = "";
        public string Nickname
        {
            get { return nickname; }
            set { nickname = value; }
        }


        private string siteid;

        public string SITEID
        {
            get { return siteid; }
            set { siteid = value; }
        }

        public cMapping.MAPPING_TYPE MAPPING_TYPE
        {
            get { return cMapping.MAPPING_TYPE.TEAMID; }
        }

        private SITE_MAPPING site_mapping = SITE_MAPPING.SCORESWAY;

        public SITE_MAPPING SITE_MAPPING
        {
            get { return site_mapping; }
            set { site_mapping = value; }
        }
        private string localid;

        public string LOCALID
        {
            get { return localid; }
            set { localid = value; }
        }

        public cTeam()
        {
        }

        public cTeam(string websiteid, string slocalid)
        {
            SITEID = websiteid;
            LOCALID = slocalid;
        }
       
        public string checkncreateTeam(string externalid, string teamname, SITE_MAPPING sm)
        {
            
            string sql = string.Format("select localid from tbmapping where {0}='{1}' and mappingtype='TEAMID'", new cMapping().getFieldMapping(sm), externalid);
            string slocalid = Dbserver.ExecuteScalar(sql).ToString();
            if (slocalid.Trim() == string.Empty) 
            {
                cMapping oMaping=new cMapping();
                oMaping.CreateNewMapping(cMapping.MAPPING_TYPE.TEAMID,LibCode.SITE_MAPPING.SCORESWAY,localid,externalid,ref slocalid);
                this.LOCALID = slocalid;
                this.nickname = teamname;
                if (CreateNewTeam())
                { return slocalid; }
                else
                { return ""; }               

            }
            else
            {
                return slocalid;
           }
        }
        private bool CreateNewTeam()
        {
            DataTable dt = new DataTable();
            getTableStructure(ref dt, TEAM_HEADER_NAME);
            DataRow dr = dt.NewRow();
            dr["TEAMID"] = LOCALID;
            dr["TEAMNAME"] = Nickname;
            //dr["RECID"] = 0;
            dr["DTCREATED"] = DateTime.Now;


            dt.Rows.Add(dr);
            string serr = "";
            try
            {
                if (Dbserver.openConn())
                {
                    bool bret = Dbserver.SqlUpdate(dt, TEAM_HEADER_NAME, out serr) > 0;
                    Dbserver.CommitAndCloseConn();
                    return bret;

                }
            }
            catch (Exception ex)
            {

                Dbserver.ForceRollBackNClose();
            }
            return false;
        }
        public bool savePlayer()
        {
            return savePlayer(SITEID);
        }

        public bool savePlayer(string SITE_ID)
        {
            LOCALID = (oMapping.getLocalCode(MAPPING_TYPE, SITE_MAPPING, SITE_ID));
            if (LOCALID != string.Empty)
            {
                if (isRecordExist(TEAM_HEADER_NAME, new string[] { "TEAMID" }, new string[] { LOCALID }))
                {
                    //string sql=string.Format("select * from {0} where PLAYERID='{1}'",PLAYER_HEADER_TABLE_NAME,LOCALID);
                    string serr = "";
                    EditRowDtDB(out serr, TEAM_HEADER_NAME, new string[] { "TEAMID" }, new string[] { LOCALID }, false,
                        new object[] { "[SKIP]", LOCALID, Nickname, "[SKIP]", DateTime.Now });



                }
                else
                {
                    return CreateNewTeam();
                }


            }
            else
            {
                oMapping.CreateNewMapping(MAPPING_TYPE, SITE_MAPPING, LOCALID, SITEID);
                LOCALID = oMapping.getLocalCode(MAPPING_TYPE, SITE_MAPPING, SITEID);

                return CreateNewTeam();
            }
            return false;

        }



    }
}
